﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace employee.Forms
{
    public partial class employeeFind : baseClass.Forms.dataFind 
    {
        private employeeEdit myEmployeeNew = null;

        public employeeFind()
        {
            InitializeComponent();
            this.employeeGrid.SetDataSource(myBaseDS.employee);
            this.employeeGrid.myOnError += new common.myEventHandler(ErrorHandler);
            this.myFullMode = false;
        }
        public data.baseDS.employeeRow mySelectedDataRow = null;

        public override bool LoadData()
        {
            myBaseDS.employee.Clear();
            DataAccess.DataLibs.LoadData(myBaseDS.employee, emCriteria.GetOptions());
            ShowReccount(myBaseDS.employee.Count);
            return true;
        }

        protected override common.controls.baseDataGridView CreateDataGrid()
        {
            return this.employeeGrid;
        }

        public override bool Find(string code, bool force)
        {
            bool HaveMarker = common.SysLibs.HaveFindMarker(ref code);
            code = code.Trim();
            this.mySelectedDataRow = null;

            //Detect FORCE mode : when finding code contains charcter ? 
            if (common.StringLibs.IsNullOrEmpty(code) || HaveMarker) force = true;

            if (force == false)
            {
                DataAccess.DataLibs.EmployeeValues options = new DataAccess.DataLibs.EmployeeValues();
                options.Code = code;
                myBaseDS.employee.Clear();
                DataAccess.DataLibs.LoadData(myBaseDS.employee, options);
                if (myBaseDS.geStatement.Count >= 1)
                {
                    this.mySelectedDataRow = myBaseDS.employee[0];
                    return true;
                }
            }
            this.myFullMode = (myBaseDS.employee.Count > 0);
            this.Visible = false;
            this.ShowDialog();
            return (this.mySelectedDataRow != null);
        }
        protected override void DoSelect()
        {
            if (this.employeeGrid.myEditDataSource.Current == null) return;
            this.mySelectedDataRow = (data.baseDS.employeeRow)((DataRowView)this.employeeGrid.myEditDataSource.Current).Row;

            DataAccess.DataCache.AddToCache(this.mySelectedDataRow);
            this.Close();
        }

        private void newBtn_Click(object sender, EventArgs e)
        {
            if (myEmployeeNew == null || myEmployeeNew.IsDisposed) myEmployeeNew = new employeeEdit();
            myEmployeeNew.ShowEmployee(null);
        }
    }
}
